﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ParseTimetable
{
    public partial class Form2 : Form
    {
        int step = 1;
        int xyIndex = 0;
        int ddlXZIndex = 0;
        int zyIndex = 0;
        int kbIndex = 0;
        int njIndex = 0;
        bool isTry = true;
        bool hasData = false;
        List<ClassTime> allClassTime = new List<ClassTime>();
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ClassTime.InsertClassTime(allClassTime);
      
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (hasData&&!isTry)
            {
                allClassTime.Add(ParseDataFromPage.GetClassTime(webBrowser1.Document));
                hasData = false;
               
            }
            bool backtracking = true;
            while (backtracking)
            {
                backtracking = false;
                if (step == 1)
                {
                    HtmlElementCollection njArray = webBrowser1.Document.GetElementById("nj").Children;
                    if (njIndex < 3)
                    {
                        this.webBrowser1.Document.GetElementById("nj").Children[njIndex].SetAttribute("selected", "selected");
                        this.webBrowser1.Document.GetElementById("nj").InvokeMember("onchange");
                        njIndex++;
                        step++;
                    }
                }
                else if (step == 2)
                {
                    HtmlElementCollection xyArray = webBrowser1.Document.GetElementById("xy").Children;
                     if (xyIndex == xyArray.Count - 1 || xyArray.Count == 0 || xyArray[xyIndex].InnerHtml == null)
                    {
                        xyIndex = 0;
                        step--;
                        backtracking = true;

                    }
                    else if (xyIndex < xyArray.Count)
                    {
                        this.webBrowser1.Document.GetElementById("xy").Children[xyIndex].SetAttribute("selected", "selected");
                        this.webBrowser1.Document.GetElementById("xy").InvokeMember("onchange");
                        xyIndex++;
                        step++;
                    }
                }
                else if (step == 3)
                {
                    HtmlElementCollection ddlXZArray = webBrowser1.Document.GetElementById("ddlXZ").Children;
                    if (ddlXZIndex == ddlXZArray.Count - 1 || ddlXZArray.Count == 0 || ddlXZArray[ddlXZIndex].InnerHtml == null)
                    {
                        ddlXZIndex = 0;
                        step--;
                        backtracking = true;

                    }
                    else if (ddlXZIndex < ddlXZArray.Count)
                    {
                        this.webBrowser1.Document.GetElementById("ddlXZ").Children[ddlXZIndex].SetAttribute("selected", "selected");
                        this.webBrowser1.Document.GetElementById("ddlXZ").InvokeMember("onchange");
                        ddlXZIndex++;
                        step++;
                    }
                }
                else if (step == 4)
                {
                    HtmlElementCollection zyArray = webBrowser1.Document.GetElementById("zy").Children;
                    if (zyIndex == zyArray.Count - 1 || zyArray.Count == 0 || zyArray[zyIndex].InnerHtml == null)
                    {
                        zyIndex = 0;
                        step--;
                        backtracking = true;

                    }
                    else if (zyIndex < zyArray.Count)
                    {
                        this.webBrowser1.Document.GetElementById("zy").Children[zyIndex].SetAttribute("selected", "selected");
                        this.webBrowser1.Document.GetElementById("zy").InvokeMember("onchange");
                        zyIndex++;
                        step++;
                        isTry = true;

                    }
                }
                else if (step == 5)
                {
                    HtmlElementCollection kbArray = webBrowser1.Document.GetElementById("kb").Children;
                    if ( kbArray.Count == 0 || kbArray[kbIndex].InnerHtml == null)
                    {
                        kbIndex = 0;
                        step--;
                        backtracking = true;

                    }
                    else if (kbIndex < kbArray.Count-1)
                    {
                        if (kbIndex == 0 && isTry == true)
                        {
                            isTry = false;
                            this.webBrowser1.Document.GetElementById("kb").Children[kbArray.Count - 1].SetAttribute("selected", "selected");
                            this.webBrowser1.Document.GetElementById("kb").InvokeMember("onchange");
                        }
                        else
                        {
                            this.webBrowser1.Document.GetElementById("kb").Children[kbIndex].SetAttribute("selected", "selected");
                            this.webBrowser1.Document.GetElementById("kb").InvokeMember("onchange");
                            hasData = true;
                            kbIndex++;
                        }


                    }

                }
            }


            
        }
    }
}
